//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Managers;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Category manager
    /// </summary>
    public class CategoryManager
    {
        #region Constants
        private const string CATEGORIES_ALL_KEY = "Nop.category.all-{0}-{1}";
        private const string CATEGORIES_BY_ID_KEY = "Nop.category.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Marks category as deleted
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        public static void MarkCategoryAsDeleted(int CategoryID)
        {
            Category category = GetByCategoryID(CategoryID);
            if (category != null)
            {
                category = UpdateCategory(category.CategoryID, category.Name, category.Description, category.TemplateID, category.MetaKeywords,
                     category.MetaDescription, category.MetaTitle, category.SEName, category.ParentCategoryID,
                     category.PictureID, category.Published, true, category.DisplayOrder,
                     category.CreatedOn, category.UpdatedOn);
            }

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(CATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(CATEGORIES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Removes category picture
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        public static void RemoveCategoryPicture(int CategoryID)
        {
            Category category = GetByCategoryID(CategoryID);
            if (category != null)
            {
                UpdateCategory(category.CategoryID, category.Name, category.Description, category.TemplateID, category.MetaKeywords,
                   category.MetaDescription, category.MetaTitle,  category.SEName, category.ParentCategoryID,
                   0, category.Published, category.Deleted, category.DisplayOrder,
                   category.CreatedOn, category.UpdatedOn);
            }

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(CATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(CATEGORIES_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets all categories
        /// </summary>
        /// <param name="ParentCategoryID">Parent category identifier</param>
        /// <returns>Category collection</returns>
        public static CategoryCollection GetAllCategories(int ParentCategoryID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(CATEGORIES_ALL_KEY, showHidden,ParentCategoryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (CategoryCollection)obj2;
            }

            CategoryCollection categoryCollection = CategoryDBManager.GetAllCategories(ParentCategoryID, showHidden);
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, categoryCollection);
            }
            return categoryCollection;
        }

        /// <summary>
        /// Gets a category
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        /// <returns>Category</returns>
        public static Category GetByCategoryID(int CategoryID)
        {
            string key = string.Format(CATEGORIES_BY_ID_KEY, CategoryID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (Category)obj2;
            }

            Category category = CategoryDBManager.GetByCategoryID(CategoryID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, category);
            }
            return category;
        }

        /// <summary>
        /// Gets a category breadcrumb
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        /// <returns>Category</returns>
        public static CategoryCollection GetBreadCrumb(int CategoryID)
        {
            CategoryCollection breadCrumb = new CategoryCollection();
            Category category = GetByCategoryID(CategoryID);
            while (category != null)
            {
                breadCrumb.Add(category);
                category = category.ParentCategory;
            }
            breadCrumb.Reverse();
            return breadCrumb;
        }

        /// <summary>
        /// Inserts category identifier
        /// </summary>
        /// <param name="Name">The category name</param>
        /// <param name="Description">The description</param>
        /// <param name="TemplateID">The template identifier</param>
        /// <param name="MetaKeywords">The meta keywords</param>
        /// <param name="MetaDescription">The meta description</param>
        /// <param name="MetaTitle">The meta title</param>
        /// <param name="SEName">The search-engine name</param>
        /// <param name="ParentCategoryID">The parent category identifier</param>
        /// <param name="PictureID">The picture identifier</param>
        /// <param name="Published">A value indicating whether the entity is published</param>
        /// <param name="Deleted">A value indicating whether the entity has been deleted</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Category</returns>
        public static Category InsertCategory(string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int ParentCategoryID, int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            Category category = CategoryDBManager.InsertCategory(Name, Description,
              TemplateID, MetaKeywords, MetaDescription, MetaTitle,
              SEName, ParentCategoryID, PictureID, Published, Deleted,
              DisplayOrder, CreatedOn, UpdatedOn);
           
            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(CATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(CATEGORIES_BY_ID_KEY);
            }

            return category;
        }

        /// <summary>
        /// Updates the category
        /// </summary>
        /// <param name="CategoryID">Category identifier</param>
        /// <param name="Name">The category name</param>
        /// <param name="Description">The description</param>
        /// <param name="TemplateID">The template identifier</param>
        /// <param name="MetaKeywords">The meta keywords</param>
        /// <param name="MetaDescription">The meta description</param>
        /// <param name="MetaTitle">The meta title</param>
        /// <param name="SEName">The search-engine name</param>
        /// <param name="ParentCategoryID">The parent category identifier</param>
        /// <param name="PictureID">The picture identifier</param>
        /// <param name="Published">A value indicating whether the entity is published</param>
        /// <param name="Deleted">A value indicating whether the entity has been deleted</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <param name="CreatedOn">The date and time of instance creation</param>
        /// <param name="UpdatedOn">The date and time of instance update</param>
        /// <returns>Category</returns>
        public static Category UpdateCategory(int CategoryID, string Name, string Description,
            int TemplateID, string MetaKeywords, string MetaDescription, string MetaTitle,
            string SEName, int ParentCategoryID,int PictureID, bool Published, bool Deleted,
            int DisplayOrder, DateTime CreatedOn, DateTime UpdatedOn)
        {
            if (CategoryID == ParentCategoryID)
                ParentCategoryID = 0;

            Category category = CategoryDBManager.UpdateCategory(CategoryID, Name, Description,
             TemplateID,  MetaKeywords,  MetaDescription,  MetaTitle,
             SEName, ParentCategoryID, PictureID, Published, Deleted,
             DisplayOrder,  CreatedOn,  UpdatedOn);

            if (NopContext.IsCachable)
            {
                bool showHidden = NopContext.Current.IsAdmin;
                NopCache.RemoveByPattern(CATEGORIES_ALL_KEY);
                NopCache.RemoveByPattern(CATEGORIES_BY_ID_KEY);
            }

            return category;
        }
        #endregion
    }
}
